package com.example.sample.control;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import net.liteheaven.xblib.widget.Toast.SmartToast;
import net.liteheaven.xblib.widget.Toast.ToastFactory;

public class MImageView extends ImageView {
	private int widthMeasureSpec;
	private int heightMeasureSpec;

	public MImageView(Context context) {
        super(context);
        init();
    }

    public MImageView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    	init();
    }
    
    public MImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    	super(context, attrs, defStyleAttr);
    	init();
    }
    
    private void init(){
    	setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SmartToast.cancelLastToast();
				int wm = MeasureSpec.getMode(widthMeasureSpec);
				int ws = MeasureSpec.getSize(widthMeasureSpec);
				String wmStr = getModeStr(wm);
				int hm = MeasureSpec.getMode(heightMeasureSpec);
				int hs = MeasureSpec.getSize(heightMeasureSpec);
				String hmStr = getModeStr(hm);
				String content = "wmode="+wmStr+"\nwsize="+ws+"\nhmode="+hmStr+"\nhsize="+hs;
                ToastFactory.makeText(getContext(), content, Toast.LENGTH_SHORT, 1).show();
			}
		});
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    	if (this.widthMeasureSpec == 0){
    		this.widthMeasureSpec = widthMeasureSpec;
    	}
    	if (this.heightMeasureSpec == 0){
        	this.heightMeasureSpec = heightMeasureSpec;
    	}
    	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    	super.onDraw(canvas);
    	int id = getId();
    	TextPaint paint = new TextPaint();
    	paint.setTextSize(5f);
    	paint.setColor(Color.parseColor("red"));
    	canvas.drawText(""+id, 0, 0, paint);
    }
    
    public String getModeStr(int val){
    	String result = "";
    	switch (val) {
		case MeasureSpec.AT_MOST:
			result = "AT_MOST";
			break;
		case MeasureSpec.EXACTLY:
			result = "EXACTLY";
			break;
		case MeasureSpec.UNSPECIFIED:
			result = "UNSPECIFIED";
			break;
		default:
			result = "UNKNOWN";
			break;
		}
    	return result;
    }
}
